<?php
/**
 * @file $HeadURL: imagesource.php $
 * @author $Author: LiangJian (liangjian@haibao.com) $
 * @date $Date: 2015-1-5 上午10:00:45 $
 * @brief 
 */
namespace haibao\cms\business;

class ImageSource extends \haibao\cms\business\Base{
	
	/**
	 * 根据图片来源名称快速联想
	 */
	public static function getSourcesByName($name,$isReal = false){
		$data = new \haibao\cms\data\ImageSource();
		return $data->getSourcesByName($name,$isReal);
	}
	
	public static function getSourceNameById($id){
		$data = new \haibao\cms\data\ImageSource();
		return $data->getOneById(intval($id));
	}
	
	public static function getSourceByName($name){
		$data = new \haibao\cms\data\ImageSource();
		return $data->getSourceByName($name);
	}
	
	/**
	 * 添加图片来源，如果要添加的来源已经存在则返回已经存在的ID和Name
	 * @param \haibao\cms\model\data\ImageSource $model
	 * @return array
	 */
	public static function addSource($model){
		if(!$model->Name){
			return false;
		}
		if ($source = self::getSourceByName($model->Name)){
			return array(
				\haibao\cms\model\filter\ImageSource::CONDITION_FIELD_NAME_ID => $source->Id,
				\haibao\cms\model\filter\ImageSource::CONDITION_FIELD_NAME_NAME => $source->Name,
			);
		}
		
		$data = new \haibao\cms\data\ImageSource();
		$data->addSource($model);
		return array(
			\haibao\cms\model\filter\ImageSource::CONDITION_FIELD_NAME_ID => $model->Id,
			\haibao\cms\model\filter\ImageSource::CONDITION_FIELD_NAME_NAME => $model->Name,
		);
	}
}